<!DOCTYPE html>
<html>
<head>
<#include "/layout/publicres.html"/>
</head>
<body>

	<form id="ffemp" method="post" style="margin:10px;">
		<div style="width:100%;margin:0 auto">
				<table id="tt" style="width:99%;height:auto;"></table> 
		</div>
		<div>
			<div id="handle_btn" align="center" style="text-align: center;padding-top:10px;">
				<a id="sub" href="javascript:void(0)" class="easyui-linkbutton"
					data-options="iconCls:'icon-save'">保存</a>
				<a onclick="checkAll()" href="javascript:void(0)" class="easyui-linkbutton"
					data-options="iconCls:'icon-reload'">全选</a>
				<a onclick="cancleAll()" href="javascript:void(0)" class="easyui-linkbutton"
					data-options="iconCls:'icon-clear'">重置</a>
			</div>
		</div>
	</form>
	<script type="text/javascript">
		function initField(value){
			var per=value.split("_")[0];
			var name=value.split("_")[1];
			if("${roleInfo.buttonsIds}".indexOf(per) !=-1){
				return '<input checked="checked" name="actorCheck" value="'+per+'" style="width:20;" type="checkbox" />'+name;
			}else{
				return '<input name="actorCheck" value="'+per+'" style="width:20;" type="checkbox" />'+name;
			}
			
		}
		function checkAll(){
	      $(':checkbox').prop('checked',true);
	    }
	    function cancleAll() {
	      $(':checkbox').prop('checked',false);
	    }
		$(function() {
			
			$('#tt').treegrid({    
			    url:'menu/roleMenuBind.html',    
			    idField:'id',    
			    treeField:'name',
			    border:true,
// 			    checkbox:true,
			    onlyLeafCheck:true,
			    singleSelect:false,
// 			    checkOnSelect:true,
// 			    selectOnCheck:true,
			    columns:[[    
			        {title:'菜单名称',field:'name',width:180},    
			        {field:'id',title:'菜单权限',width:60,formatter: function(value,row,index){
			        	
			        	var len = $('#tt').treegrid('getChildren', row.id);
			        	if(len.length <= 0){
			        		if("${roleInfo.menuIds}".indexOf(value) !=-1){
				        		return '<input  name="menuField" checked="checked" value="'+row.id+'" style="width:20;" type="checkbox" />读取';
				        	}else{
				        		return '<input  name="menuField" value="'+row.id+'" style="width:20;" type="checkbox" />读取';
				        	}
			        	}
			        	
			        }},    
			        {field:'A1',title:'按钮1',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }},    
			        {field:'A2',title:'按钮2',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }},    
			        {field:'A3',title:'按钮3',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }},    
			        {field:'A4',title:'按钮4',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }},    
			        {field:'A5',title:'按钮5',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }},    
			        {field:'A6',title:'按钮6',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }},    
			        {field:'A7',title:'按钮7',width:60,formatter: function(value,row,index){
			        	if(value!=undefined){
				        	return initField(value);
			        	}
			        }}
			    ]],
			    onLoadSuccess:function(row ,data){
// 			    	if(row.id == 2){
// 			    		$('#tt').treegrid('select',2);
// 			    		$('#tt').treegrid('select',4);
// 			    		$('#tt').treegrid('select',6);
// 			    	}
			    }
			});
			$("#sub").bind(
					"click",
					function() {
						$.messager.progress();
						var checkList = document.getElementsByName("actorCheck");
						//按钮数组
						var array_btn = new Array();
						for (var i = 0; i < checkList.length; i++) {
							console.log(checkList[i].value);
							if(checkList[i].checked == true){
								array_btn.push(checkList[i].value);
							}
						}
						var mList = document.getElementsByName("menuField");
						//菜单数组
						var array_menu = new Array();
						for (var i = 0; i < mList.length; i++) {
							if(mList[i].checked == true){
								array_menu.push(mList[i].value);
							}
						}
						if(array_btn.length==0 && array_menu.length == 0){
							$.messager.progress('close');
							$.messager.alert("操作提示", "请选择具体权限", "info");
							return false;
						}
						$.ajax({
								type : "POST",
								url : "role/savePermission",
								data : {
									"btnArr":array_btn.toString(),
									"menuArr":array_menu.toString(),
									"roleId":"${roleInfo.roleId}"
								},
								success : function(msg) {
									data = eval('(' + msg + ')');
									$.messager.progress('close');
									if (data.result) {
										$.messager.alert("操作提示", "保存成功","info");
									} else {
										$.messager.alert("操作提示", data.msg,"error");
									}
									$('#perset-dialog').window('close');
								}
						});
					});
		});
	</script>
</body>
</html>
